/**
 * Created by MGL on 2017/6/16.
 */

import com.example.ICalculator;
import org.junit.Test;

import java.lang.reflect.Method;

public class Test01 {
    @Test
    public void testFileSystemClassLoader() {
        String classDataRootPath = "E:\\javadaima\\source\\classloader\\out\\production\\classloader";
        FileSystemClassLoader fscl1 = new FileSystemClassLoader(classDataRootPath);
        FileSystemClassLoader fscl2 = new FileSystemClassLoader(classDataRootPath);
        String className = "com.example.Sample";
        try {
            Class<?> class1 = fscl1.findClass(className);
            ClassLoader classLoader = class1.getClassLoader();
            Object obj1 = class1.newInstance();
            Class<?> class2 = fscl2.findClass(className);
            Object obj2 = class2.newInstance();
            Method setSampleMethod = class1.getMethod("setSample", java.lang.Object.class);
            setSampleMethod.invoke(obj1, obj2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testNetworkClassLoader() {
        String url = "http://localhost:8080/tree/";
        NetworkClassLoader ncl = new NetworkClassLoader(url);
        String basicClassName = "com.example.CalculatorBasic";
        String advancedClassName = "com.example.CalculatorAdvanced";
        try {
            Class<?> clazz = ncl.findClass(basicClassName);
            ICalculator calculator = (ICalculator) clazz.newInstance();
            System.out.println(calculator.getVersion());
            clazz = ncl.findClass(advancedClassName);
            calculator = (ICalculator) clazz.newInstance();
            System.out.println(calculator.getVersion());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
